define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/index',
                    add_url: 'business/add',
                    edit_url: 'business/edit',
                    del_url: 'business/del',
                    multi_url: 'business/multi',
                    table: 'business',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'business_id',
                sortName: 'business_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'business_id', title: __('商家ID')},
                        {field: 'business_phone', title: __('手机号')},
                        {field: 'business_title', title: __('公司名称')},
                        {field: 'link_phone', title: __('Link_phone')},
                        {
                            field: 'business_img', 
                            title: __('Business_img'),
                            formatter: Controller.api.formatter.img
                        },
                        {field: 'business_address', title: __('Business_address')},
                        {field: 'cert_status_text', title: __('认证状态')},
                        {field: 'cash_amount', title: __('账户余额')},
                        {field: 'status_text', title: __('用户状态')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'business_id', 
                            title: __('操作'), 
                            operate: false, 
                            width: '110px',
                            formatter: Controller.api.formatter.opt
                        },
//                        {field: 'checktime', title: __('Checktime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
//                        {field: 'refusal_reason', title: __('Refusal_reason')},
//                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            
            $(table).on("click", "[data-id].btn-resetpassword", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                var url = $(that).data("url");
                Layer.confirm(
                    __('确定要重置密码吗？'),
                    {icon: 3, title: __('提示'), shadeClose: true},
                    function (index) {
                        $.post(url,{task_id:id},function(res){
                            Layer.msg('密码已重置');
                            table.bootstrapTable('refresh', {});
                        });
                        Layer.close(index);
                    }
                );
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {//渲染的方法
                img: function (value, row, index) {
                    var html = '<a href="'+value+'" target="_blank"><img src="'+value+'" style="max-width: 80px; max-height: 60px;" /></a>';
                    return html;
                },
                opt:function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var btn = row.status == 'normal' ? '<b>禁止</b>' : '<b style="color: #72afd2;">开启</b>';
                    var html = '<a class="btn-change text-success" style="cursor:pointer" data-url="business/updatestatus" data-id="'+ row.admin_id + '">'+btn+'</a>';
                    html += ' <a class="btn-resetpassword text-success" style="cursor:pointer" data-url="business/resetpassword" data-id="'+ row.admin_id + '"><b>重置密码</b></a>';
                    html += '<a href="business/detail/ids/'+value+'" class="btn-change text-success" style="margin-left: 10px;" title="商家信息" data-table-id="table" data-field-index="11" data-row-index="0" data-button-index="2"><b>商家信息</b></a>';
                    return html;
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                
                $(document).on("click", ".btn-setwallet", function (e) {
//                    e.preventDefault();
                    layer.open({
                      type: 1,
                      title: '调整钱包金额',
                      maxmin: true,
                      area: ['600px', '400px'],
                      content: Template('setwallet', {}),
                      end: function(){
                      }
                    });
                });
            }
        }
    };
    return Controller;
});
